﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.uicontrols;

namespace MailQueueUmbracoAdmin
{
    public partial class MailQueueLog : System.Web.UI.UserControl
    {
        protected TabView _tabViewContainer = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            string parLabel = Request.QueryString["label"];
            string parStatus = Request.QueryString["status"];

            mailQueueDataSource.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["mailqueue"].ConnectionString;

            string sqlCmdText = "SELECT [MailQueueID] " +
                                          ",[MailCreateTime] " +
                                          ",[MailProcessTime] " +
                                          ",[MailStatus] " +
                                          ",[MailProcessId] " +
                                          ",[FromName] " +
                                          ",[FromMailAddress] " +
                                          ",[ToMailAddress] " +
                                          ",[CCList] " +
                                          ",[BccList] " +
                                          ",[Subject] " +
                                          ",[Priority] " +
                                          ",[MailFormat] " +
                                          ",[Label] " +
                                      "FROM [MailQueue] ";

            if ((parLabel != null && parLabel.Length > 0) || (parStatus != null && parStatus.Length > 0))
            {
                string sqlFilter = "";

                if (parLabel != null && parLabel.Length > 0)
                    sqlFilter += " Label = '{0}'";

                if (parStatus != null && parStatus.Length > 0)
                {
                    if (parLabel != null && parLabel.Length > 0)
                        sqlFilter += " AND MailStatus = {1} ";
                    else
                        sqlFilter += " MailStatus = {0} ";
                }

                mailQueueDataSource.FilterExpression = sqlFilter;
            }

            sqlCmdText += "ORDER BY MailCreateTime DESC ";
            mailQueueDataSource.SelectCommand = sqlCmdText;

            if (parLabel != null && parLabel.Length > 0)
            {
                string lbl = parLabel.Equals("[No label]") ? "" : parLabel;
                mailQueueDataSource.FilterParameters.Add("Label", lbl);
            }

            if (parStatus != null && parStatus.Length > 0)
            {
                mailQueueDataSource.FilterParameters.Add("MailStatus", parStatus);
            }



            queueLogGrid.DataKeyNames = new string[] { "MailQueueID" };
        }

        protected void queueLogGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
        {

            // If multiple ButtonField column fields are used, use the
            // CommandName property to determine which button was clicked.
            if (e.CommandName == "viewDetails")
            {
                // Convert the row index stored in the CommandArgument
                // property to an Integer.
                int index = Convert.ToInt32(e.CommandArgument);

                //Guid keyVal = (Guid)queueLogGrid.DataKeys[0].Value;
                //Guid keyVal = Guid.NewGuid();
                Guid keyVal = new Guid(Convert.ToString(queueLogGrid.DataKeys[index].Value));

                if (TabViewContainer != null)
                {
                    TabPage detailsTab = TabViewContainer.NewTabPage("Message details");
                    detailsTab.HasMenu = false;

                    MailQueueItemDetails detailsTabContent = (MailQueueItemDetails)LoadControl("MailQueueItemDetails.ascx");
                    detailsTabContent.TabViewContainer = TabViewContainer;
                    detailsTabContent.UniqueMailId = keyVal;
                    detailsTab.Controls.Add(detailsTabContent);
                }
            }
        }

        protected void queueLogGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ////Show status
                //Image stImg = (Image)e.Row.Cells[1].Controls[0];
                //int s = 0;
                //Int32.TryParse(e.Row.Cells[8].Text, out s);
                //switch (s)
                //{
                //    case 1:
                //        stImg.ImageUrl = "~/umbraco/images/umbraco/BlueSquare16.png";
                //        break;
                //    case 2:
                //        stImg.ImageUrl = "~/umbraco/images/umbraco/GreenSquare16.png";
                //        break;
                //    case 4:
                //        stImg.ImageUrl = "~/umbraco/images/umbraco/YellowSquare16.png";
                //        break;
                //    case 8:
                //        stImg.ImageUrl = "~/umbraco/images/umbraco/RedSquare16.png";
                //        break;
                //}

                ////Show priority
                //Image mpImg = (Image)e.Row.Cells[2].Controls[0];
                //int p = 0;
                //Int32.TryParse(e.Row.Cells[9].Text, out p);
                //System.Net.Mail.MailPriority mp = (System.Net.Mail.MailPriority)p;
                //switch (mp)
                //{
                //    case System.Net.Mail.MailPriority.High:
                //        mpImg.ImageUrl = "~/umbraco/images/umbraco/HighPriority16.png";
                //        break;
                //    case System.Net.Mail.MailPriority.Low:
                //        mpImg.ImageUrl = "~/umbraco/images/umbraco/LowPriority16.png";
                //        break;
                //    default:
                //        mpImg.Visible = false;
                //        break;
                //}

                

                int subjectCell = 6;
                LinkButton l = (LinkButton)e.Row.Cells[subjectCell].Controls[0];

                if (l.Text.Length == 0)
                {
                    l.Text = "[No subject]";
                }
            }
        }

        public TabView TabViewContainer
        {
            get { return _tabViewContainer; }
            set { _tabViewContainer = value; }
        }
    }
}